/* Copyright (c) 2008-2010 -- CommonsWare, LLC

	 Licensed under the Apache License, Version 2.0 (the "License");
	 you may not use this file except in compliance with the License.
	 You may obtain a copy of the License at

		 http://www.apache.org/licenses/LICENSE-2.0

	 Unless required by applicable law or agreed to in writing, software
	 distributed under the License is distributed on an "AS IS" BASIS,
	 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	 See the License for the specific language governing permissions and
	 limitations under the License.
*/
	 
package com.commonsware.android.maps;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MapView.LayoutParams;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.OverlayItem;

import java.security.acl.LastOwnerException;
import java.util.ArrayList;
import java.util.List;

public class NooYawk1 extends MapActivity {
	private MapView map=null;
	private MyLocationOverlay me=null;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		Intent i = getIntent();
		
	    String latS=i.getStringExtra("lat");
	    if (latS == null) latS = "0.0";
        double lat=Double.parseDouble(latS);
        
	    String lonS=i.getStringExtra("lon");
	    if (lonS == null) lonS = "0.0";
	    double lon=Double.parseDouble(lonS);

		super.onCreate(savedInstanceState);
		setContentView(R.layout.mainmap);
		
		map=(MapView)findViewById(R.id.map);
		
		  map.getController().setCenter(getPoint(lat,lon));
		  map.getController().setZoom(17);
		  map.setBuiltInZoomControls(true);
		  
		
		Drawable marker=getResources().getDrawable(R.drawable.marker);
		
		marker.setBounds(0, 0, marker.getIntrinsicWidth(),
														marker.getIntrinsicHeight());
		
	//	map.getOverlays().add(new SitesOverlay(marker));
		
		me=new MyLocationOverlay(this, map);
		map.getOverlays().add(me);
      //  Intent i = getIntent();
        
        //String foosh = i.getStringExtra("SNARP");
		
	        }
	
	@Override
	public void onResume() {
		//System.out.println("OnResume1");

		super.onResume();
		//System.out.println("OnResume2");

		me.enableCompass();
		//System.out.println("OnResume3");

	}		
	
	@Override
	public void onPause() {
		//System.out.println("Onpause1");
		super.onPause();
		//System.out.println("Onpause2");

		me.disableCompass();
		//System.out.println("Onpause3");

	}		
	
 	@Override
	protected boolean isRouteDisplayed() {
		return(false);
	}
	
 	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_S) {
			map.setSatellite(!map.isSatellite());
			return(true);
		}
		else if (keyCode == KeyEvent.KEYCODE_Z) {
			map.displayZoomControls(true);
			return(true);
		}
		
		return(super.onKeyDown(keyCode, event));
	}

	private GeoPoint getPoint(double lat, double lon) { 
		return(new GeoPoint((int)(lat*1000000.0),
													(int)(lon*1000000.0)));
	}
		
	
	class PopupPanel {
		View popup;
		boolean isVisible=false;
		
		PopupPanel(int layout) {
			ViewGroup parent=(ViewGroup)map.getParent();

			popup=getLayoutInflater().inflate(layout, parent, false);
									
			popup.setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {
					hide();
				}
			});
		}
		
		View getView() {
			return(popup);
		}
		
		void show(boolean alignTop) {
			RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(
						RelativeLayout.LayoutParams.WRAP_CONTENT,
						RelativeLayout.LayoutParams.WRAP_CONTENT
			);
			
			if (alignTop) {
				lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
				lp.setMargins(0, 20, 0, 0);
			}
			else {
				lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
				lp.setMargins(0, 0, 0, 60);
			}
			
			hide();
			
			((ViewGroup)map.getParent()).addView(popup, lp);
			isVisible=true;
		}
		
		void hide() {
			if (isVisible) {
				isVisible=false;
				((ViewGroup)popup.getParent()).removeView(popup);
			}
		}
	}
}